import Vue from 'vue'
//import store from './store'
import App from './App'
import * as filters from '@/common/filters/filter'
import store from './store/index'
import $mAssetsPath from './common/assets.config.js'
import tim from './common/tim/tim.js'
import commen from './common/commen.js'
import TIM from './components/tim-js-sdk/tim-js.js'
import prePage  from '@/common/util.js';
import $power  from '@/common/power.js';//权限设置
import DateFormat from 'common/DateFormat.js'

Vue.config.productionTip = false
Vue.prototype.tim = tim.tim  			//tim sdk 引入后生成的tim服务
Vue.prototype.$TIM = TIM				//tim 的状态/事件 常量
Vue.prototype.$commen = commen
Vue.prototype.DateFormat = DateFormat; 


// 考虑规范性,使用 module.exports 导出,应该使用 require 引用
Vue.config.productionTip = false

// 全局顶级容器组件
import jstThemeContent from 'components/jst-theme-content.vue'
Vue.component('jst-view',jstThemeContent);



//Vue.prototype.$store = store;
Vue.prototype.version = '1.0.2'; 
Vue.prototype.$mAssetsPath = $mAssetsPath;
Vue.prototype.$util = prePage;
Vue.prototype.$power= $power;

Object.keys(filters).forEach(key=>{
	Vue.filter(key,filters[key])
})

//#ifdef APP-PLUS
var domModule = weex.requireModule('dom');
domModule.addRule('fontFace', {
  'fontFamily': "iconfont",
  'src': "url('https://at.alicdn.com/t/font_1404646_cy1ar41q2ml.ttf')"
})
//#endif
App.mpType = 'app'
const app = new Vue({
    ...App,
	store
})
app.$mount()


/**
 * 格式化时间戳(特殊)
 * @param {Object} time
 * return (?天前/?月前/?年前)
 * 
 */
Vue.filter('formatDate', function(time) {
	if (time) {
		var date = new Date(time);
		var difftime = Math.abs(new Date() - date)
		// 获取当前时间的年月
		var nowyear = date.getFullYear();
		var nowmonth = date.getMonth + 1;
		var yearAllday = 0;
		var monthAllday = 0;
		// 判断是否为闰年
		if ((nowyear % 4 === 0 && nowyear % 100 !== 0) || nowyear % 400 === 0) {
			yearAllday = 366
		} else {
			yearAllday = 365
		}
		// 每个月的天数
		if (yearAllday === 366 && nowmonth === 2) {
			monthAllday = 29
		} else if (yearAllday === 365 && nowmonth === 2) {
			monthAllday = 28
		}
		if (nowmonth === 4 || nowmonth === 6 || nowmonth === 9 || nowmonth === 11) {
			monthAllday = 30
		} else {
			monthAllday = 31
		}
		if (new Date(time).toDateString() === new Date().toDateString()) { //今天
		    console.log("当天");
			// var time = newData - diffTime;
			let wu = '';
			let tt = new Date(time);
			var hours = tt.getHours();
			if(hours>0 && hours<= 12){
				wu = '上午';
			}else if(hours>12 && hours<= 24){
				wu = '下午';
			}
			var minutes = tt.getMinutes();
			return wu + hours+':'+minutes;
			
		} else if (new Date(time) < new Date()){
		    //之前
		    console.log('昨天',new Date(time).toISOString().slice(0,10));
			var today = new Date();
			var yesterday = new Date(today - 1000 * 60 * 60 * 24);
			var test = new Date(time);
			if (yesterday.getYear() === test.getYear() && yesterday.getMonth() === test.getMonth() && yesterday.getDate() === test.getDate()) {
			   return '昨天'
			} else {
			    console.log('不是昨天');
				if (difftime > yearAllday * 24 * 3600 * 1000) {
					var yearNum = Math.floor(difftime / (yearAllday * 24 * 3600 * 1000))
					return yearNum + "年前"
				} else if (difftime > monthAllday * 24 * 3600 * 1000 && difftime < yearAllday * 24 * 3600 * 1000) { //大于一周显示日期
					let tt = new Date(time);
					var month = tt.getMonth() + 1;
					var day = tt.getDate();
					return month + '月' + day + '日';
				} else if (difftime > 7 * 24 * 60 * 60 * 1000 && difftime && difftime < monthAllday * 24 * 60 * 60 * 1000) { //大于一周显示日期
					let tt = new Date(time);
					var month = tt.getMonth() + 1;
					var day = tt.getDate();
					return month + '月' + day + '日';
				} else if (difftime < 7 * 24 * 3600 * 1000 && difftime > 24 * 3600 * 1000) { //一周之内
					// //注释("一周之内");
					if(difftime < 7 * 24 * 3600 * 1000 && difftime > 24 * 3600 * 1000){ // 7天之内：周一、周二、周三、周四、周五、周六、周日
					    var week;
						let tt = new Date(time);
					    if(tt.getDay() == 0) week = "周日"
					    if(tt.getDay() == 1) week = "周一"
					    if(tt.getDay() == 2) week = "周二"
					    if(tt.getDay() == 3) week = "周三"
					    if(tt.getDay() == 4) week = "周四"
					    if(tt.getDay() == 5) week = "周五"
					    if(tt.getDay() == 6) week = "周六"
					    return week;
					}
				}
			}
		}
	}
})

Vue.filter('DateFormat', function(time,formatStr) {
	var dateFormat = new DateFormat();
	return dateFormat.setTime(time).toString(formatStr);
})